package com.xuerhua.yxt.controller.sys.student;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xuerhua.yxt.common.RspResult;
import com.xuerhua.yxt.common.enums.Status;
import com.xuerhua.yxt.controller.BaseController;
import com.xuerhua.yxt.model.Grid;
import com.xuerhua.yxt.model.StudentInfo;
import com.xuerhua.yxt.page.Page;
import com.xuerhua.yxt.reqVo.StudentReqVO;
import com.xuerhua.yxt.service.IStudentService;
import com.xuerhua.yxt.utils.BeanUtils;
import com.xuerhua.yxt.utils.DateUtil;
import com.xuerhua.yxt.utils.ExcelExportUtil;
import com.xuerhua.yxt.utils.ExportSetInfoUtil;
import com.xuerhua.yxt.view.StudentInfoView;
import com.xuerhua.yxt.vo.StudentInfoInputVO;

@RequestMapping("sys/student/manage")
@Controller
public class SysStudentManageController extends BaseController {
	
	@Autowired
	private IStudentService studentService;
	
	@RequestMapping(value={"","/"})
	public String Index() {
		return "sys/student/manage";
	}
	
	@RequestMapping("pageQuery")
	@ResponseBody Object pageQuery(StudentReqVO vo){
		try {
			Page<StudentInfo> page = new Page<>();
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			page.setParams(vo);
			List<StudentInfo> list = studentService.selectFullListByPage(page);
			Grid grid = new Grid();
			grid.setRows(BeanUtils.copyAs(list, StudentInfoView.class));
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("addUpdate")
	@ResponseBody Object addUpdate(StudentInfoInputVO vo) {
		try {
			StudentInfo student = BeanUtils.copyAs(vo, StudentInfo.class);
			if(StringUtils.isNotBlank(vo.getBirthday())) {
				student.setBirthday(DateUtil.strToDate(vo.getBirthday(),DateUtil.DEFAULT_DATE_FORMAT3));
			}
			if(student.getId() == null) {
				studentService.addStudent(student);
			}else {
				studentService.updateById(student);
			}
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("queryById/{id}")
	@ResponseBody Object queryById(@PathVariable Integer id) {
		return studentService.selectById(id);
	}
	
	@RequestMapping("export")
	ModelAndView export(StudentReqVO vo) {
		ModelAndView modelAndView = new ModelAndView();
		try {
			Page<StudentInfo> page = new Page<>();
			page.setPageNo(vo.getPage());
			page.setPageSize(Integer.MAX_VALUE - 1);
			page.setParams(vo);
			List<StudentInfo> list = studentService.selectFullListByPage(page);
			//数据源
			LinkedHashMap<String, List> map = new LinkedHashMap<String, List>();
			String title="学生名单";
			map.put(title, BeanUtils.copyAs(list, StudentInfoView.class));
			
			//标题  和  标题变量
		    List<String[]> headNames = new ArrayList<String[]>();
		    headNames.add(new String[] { "年级名称","班号","学号","学籍号","姓名","性别",	
		    		"身份证类型","身份证号","生日","民族","国籍","户口性质","户籍类别","户籍省市区",
		    		"户籍所在详细地址","现住地址","是否独生子女","户籍类别","籍贯","出生地","爸爸姓名",	
		    		"爸爸电话","爸爸是否监护人","爸爸户口所在地","妈妈现住址","妈妈姓名","妈妈电话",	
		    		"妈妈是否监护人","妈妈户口所在地","妈妈现住址"});
		    List<String[]> fieldNames = new ArrayList<String[]>();
		    fieldNames.add(new String[] { "gradeName","clazz","studentNum","studyNo","name","sex",
		    		"idNoType","idNo","birthdayDate","nation","nationality","homeNature","placeType","placeSSQ",
		    		"placeDetail","homeAddr","onlyChild","studentPlaceType","nativePlace","birthAddr","fatherName",
		    		"fatherMobile","fatherIsGuardian","fatherPlace","fatherHomeAddr","motherName","motherMobile",
		    		"motherIsGuardian","motherPlace","motherHomeAddr"});
		    
		    //excel生成帮助类
		    List<Integer[]> columnFormat=new ArrayList<Integer[]>();
		    Integer [] format=new Integer[31];
		    columnFormat.add(format);
		    
		    String [] titles=new String[] {title};
		    //excel生成帮助类
		    ExportSetInfoUtil setInfo = new ExportSetInfoUtil(map,titles,headNames,fieldNames,columnFormat);
		    request.setAttribute("exportDatas", setInfo);
		    
		    //生成
			modelAndView=new ModelAndView(new ExcelExportUtil(title+".xls",""));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelAndView;
	}
	
}
